package chapter8.practice;

import java.util.HashMap;
import java.util.Map;

/**
 * 给定一个数组，使用数组的元素作为map的key，value保存该key在集合中出现的次数
 */
public class MapCount {
    public static void main(String[] args) {
        String[] arr = new String[] {
                "a", "b", "a", "b", "c", "a", "b", "c", "b"
        };
        Map map = new HashMap();
        for (int i = 0; i < arr.length; i++) {
            Object pre = map.put(arr[i], 1);
            if(pre != null && !pre.equals("")) {
                map.put(arr[i], (Integer) pre + 1);
            }
        }
        map.forEach((key, value)-> System.out.println("key->" + key + "在集合中出现的次数为:" + value));
    }
}
